package Java7_Concurrency_Cookbook.concurrent_custom.custom_lock;

import java.util.concurrent.TimeUnit;

/**
 * @author lixiangke
 * @date 2017/11/28
 */
public class Task implements Runnable {
    private MyLock myLock;
    private String name;

    public Task(String name, MyLock myLock) {
        this.myLock = myLock;
        this.name = name;
    }

    @Override
    public void run() {
        myLock.lock();
        System.out.printf("Zoom: %s: Take the lock\n", name);
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.printf("Zoom: %s : Free the lock.\n", name);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            myLock.unlock();
        }
    }
}
